import Ember from 'ember';
import config from 'game-front/config/environment';
import RSVP from 'rsvp';

export default Ember.Route.extend({
  model(params) {
	  var userInfo = this.modelFor("application");
    return RSVP.hash({
      userInfo: userInfo,
      address: params.id && $.getJSON(config.apiBase + "addresses/" + params.id, {userId: userInfo.userId}) || {}
    });
  },
  beforeModel: function(posts, transition) {
    Ember.run.scheduleOnce('afterRender', this, function() {
      $(".user-background").height(document.body.scrollHeight);
    });
  },
  actions: {
    create(model) {
      const address = model.address;
      const userInfo = model.userInfo;
      console.log(model);
      if (address &&　address.id) {
        $.ajax({
          method: "PUT",
          url: config.apiBase + "/addresses/" + address.id + "?userId=" + userInfo.userId,
          data: JSON.stringify(address),
          contentType:"application/json",
          dataType:"json"
        }) .done(function(resp) {
          window.location.href = config.baseURL + "mysite";
        });
      } else {
        $.ajax({
          method: "POST",
          url: config.apiBase + "/addresses?userId=" + userInfo.userId,
          data: JSON.stringify(address),
          contentType:"application/json",
          dataType:"json"
        }) .done(function(resp) {
          window.location.href = config.baseURL + "mysite";
        });
      }
    }
  }
});
